﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UtilZ.Dotnet.Ex.Communication.Net.Web;

namespace UtilZ.Tool.HostPluginBase
{
    /// <summary>
    /// 主机插件基类
    /// </summary>
    public abstract class HostPluginAbs : IHostPlugin
    {
        protected readonly string _ID;
        protected readonly string _name;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="id">插件标识</param>
        /// <param name="name">插件名称</param>
        public HostPluginAbs(string id, string name)
        {
            this._ID = id;
            this._name = name;
        }


        /// <summary>
        /// 插件标识
        /// </summary>
        public string ID
        {
            get { return this._ID; }
        }

        /// <summary>
        /// 插件名称
        /// </summary>
        public string Name
        {
            get { return this._name; }
        }

        /// <summary>
        /// 操作系统适配器,由主程序设置值
        /// </summary>
        protected IOSAdapter _OSAdapter = null;
        /// <summary>
        /// 操作系统适配器,由主程序设置值
        /// </summary>
        public IOSAdapter OSAdapter
        {
            get { return _OSAdapter; }
            set { _OSAdapter = value; }
        }




        /// <summary>
        /// 初始化插件
        /// </summary>
        public void Init()
        {
            this.PrimitiveInit();
        }

        /// <summary>
        /// 初始化插件
        /// </summary>
        protected virtual void PrimitiveInit()
        {

        }



        /// <summary>
        /// 启动
        /// </summary>
        public void Start()
        {
            this.PrimitiveStart();
        }

        /// <summary>
        /// 启动
        /// </summary>
        protected virtual void PrimitiveStart()
        {

        }



        /// <summary>
        /// 获取插件web资源信息,为null则表示无相关信息
        /// </summary>
        /// <returns>插件web资源信息,为null则表示无相关信息</returns>
        public WebResourceInfo GetWebResourceInfo()
        {
            return this.PrimitiveGetWebResourceInfo();
        }

        /// <summary>
        /// 获取插件web资源信息,为null则表示无相关信息
        /// </summary>
        /// <returns>插件web资源信息,为null则表示无相关信息</returns>
        protected virtual WebResourceInfo PrimitiveGetWebResourceInfo()
        {
            return null;
        }







        /// <summary>
        /// 发送Websocket消息
        /// </summary>
        public event EventHandler<WebsocketSendMessageArgs> SendWebsocketMessage;

        /// <summary>
        /// 触发发送Websocket消息
        /// </summary>
        /// <param name="text">消息文本</param>
        /// <param name="targets">发送目录,如果集合为空或null,则发送给插件标识对应的所有客户端</param>
        protected void OnRaiseSendWebsocketMessage(string text, IEnumerable<WebSocketClient> targets = null)
        {
            var handler = this.SendWebsocketMessage;
            if (handler != null)
            {
                handler(this, new WebsocketSendMessageArgs(this._ID, text, targets));
            }
        }

        /// <summary>
        /// WebSocket接收消息处理
        /// </summary>
        /// <param name="webSocket">WebSocketClient</param>
        /// <param name="message">接收到的消息</param>
        public void WebSocketReceiveMessage(WebSocketClient webSocket, ReceiveWebsocketMessage message)
        {
            this.PrimitiveWebSocketReceiveMessage(webSocket, message);
        }

        /// <summary>
        /// WebSocket接收消息处理
        /// </summary>
        /// <param name="webSocket">WebSocketClient</param>
        /// <param name="message">接收到的消息</param>
        protected virtual void PrimitiveWebSocketReceiveMessage(WebSocketClient webSocket, ReceiveWebsocketMessage message)
        {
            throw new NotImplementedException();
        }






        protected bool _disposabled = false;
        /// <summary>
        /// 获取当前对象是否已释放[true:已释放;false:未释放]
        /// </summary>
        public bool Disposabled
        {
            get { return _disposabled; }
        }

        /// <summary>
        /// 释放资源
        /// </summary>
        public void Dispose()
        {
            if (this._disposabled)
            {
                return;
            }
            this._disposabled = true;

            this.Dispose(true);
        }

        /// <summary>
        /// 释放资源
        /// </summary>
        /// <param name="disposing">参数标识</param>
        protected virtual void Dispose(bool disposing)
        {

        }

    }
}
